<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="75px">
      <el-form-item label="所属课程" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入课程标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="课程状态" prop="applyStatus">
        <el-select v-model="queryParams.applyStatus" placeholder="请选择课程状态" clearable>
          <el-option v-for="dict in dict.type.bmzt" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['business:platform:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['business:platform:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['business:platform:remove']">删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['business:platform:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="platformList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="baseId" v-if="false" />
      <el-table-column label="图片" align="center" prop="picLink" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.picLinkUrl" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="课程标题" align="center" prop="title" />
      <el-table-column label="课程状态" align="center" prop="applyStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bmzt" :value="scope.row.applyStatus" />
        </template>
      </el-table-column>
      <el-table-column label="售卖价格(元)" align="center" prop="sellPrice" />
      <el-table-column label="积分量(分)" align="center" prop="credits" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="操作" width="350px" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-menu" @click="chapterManagement(scope.row)">课程管理
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-user" @click="userManagement(scope.row)">用户管理
          </el-button>
          <!--<el-button size="mini" type="text" icon="el-icon-view" @click="handlePreview(scope.row)">试卷预览
          </el-button>-->
          <!--<el-button
            size="mini"
            type="text"
            icon="el-icon-video-camera"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:platform:edit']"
          >视频管理
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-notebook-2"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:platform:edit']"
          >题库管理
          </el-button>-->
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['business:platform:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['business:platform:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog :title="title" v-if="openPreview" :visible.sync="openPreview" width="1000px" append-to-body>
      <Preview :type="previewType" :id="previewId"></Preview>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openPreview=false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改课程管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="课程标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="图片" prop="picLink">
          <image-upload v-model="form.picLink" :limit="1" />
        </el-form-item>
        <el-form-item label="课程状态" prop="applyStatus">
          <el-radio-group v-model="form.applyStatus">
            <el-radio v-for="dict in dict.type.bmzt" :key="dict.value" :label="parseInt(dict.value)">{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="售卖价格" prop="sellPrice">
          <el-input v-model="form.sellPrice" @input="(v)=>(form.sellPrice=v.replace(/[^\d.]/g,''))"
            placeholder="请输入售卖价格">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="积分量" prop="credits">
          <el-input v-model="form.credits" @input="(v)=>(form.credits=v.replace(/[^\d.]/g,''))" placeholder="请输入积分量">
            <template slot="append">分</template>
          </el-input>
          <el-tag type="warning">达到积分量可参与考试</el-tag>
        </el-form-item>
        <el-form-item label="证书模板" prop="certTemplate">
          <image-upload v-model="form.certTemplate" :limit="1" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" :min="1" placeholder="请输入排序"></el-input-number>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--    -->
    <el-drawer :title="'所属课程：' + formDetail.title" :visible.sync="drawer" size="75%">
      <div class="ml20 mr20">
        <el-tabs v-model="activeName" type="" @tab-click="handleClick">
          <el-tab-pane label="基本信息" name="first">
            <el-card class="ml20 mr20">
              <div slot="header" class="clearfix">
                <span>课程基本信息</span>
              </div>
              <el-descriptions title="">
                <el-descriptions-item label="课程标题">{{ formDetail.title }}</el-descriptions-item>
                <el-descriptions-item label="课程状态">
                  <dict-tag size="mini" :options="dict.type.bmzt" :value="formDetail.applyStatus" />
                </el-descriptions-item>
                <el-descriptions-item label="售卖价格">
                  {{ formDetail.sellPrice }}元
                </el-descriptions-item>
                <el-descriptions-item label="积分量">
                  {{ formDetail.credits }}分
                  <el-tag class="ml5" size="mini" type="warning">达到积分量可参与考试</el-tag>
                </el-descriptions-item>
                <el-descriptions-item label="备注">
                  {{ formDetail.remark?formDetail.remark : '无' }}
                </el-descriptions-item>
                <!--<el-descriptions-item label="图片">
                  <image-preview :src="form.picLinkUrl" :width="350" :height="200"/>
                </el-descriptions-item>-->
              </el-descriptions>
            </el-card>
            <el-card class="ml20 mr20 mt20">
              <div slot="header" class="clearfix">
                <span>订单信息</span>
              </div>
              <OrderInfo :platformId="formDetail.baseId" :activeName="activeName"></OrderInfo>
            </el-card>
          </el-tab-pane>
          <el-tab-pane label="章节管理" name="second">
            <PlatformChapter :platformId="formDetail.baseId" :activeName="activeName"></PlatformChapter>
          </el-tab-pane>
          <!--<el-tab-pane label="视频管理" name="third">
            <PlatformVideo :platformId="formDetail.baseId" :activeName="activeName"></PlatformVideo>
          </el-tab-pane>-->
          <el-tab-pane label="试卷设置" name="fourth">
            <el-tag type="warning" class="ml20" closable>注意：同一套课程对应多套考试试卷，但最多只有一套生效！</el-tag>
            <PaperInfo :platformId="formDetail.baseId" :activeName="activeName" type="0"></PaperInfo>
          </el-tab-pane>
          <el-tab-pane label="练习试卷设置" name="fifty">
            <el-tag type="warning" class="ml20" closable>注意：同一套课程对应多套课后练习，但最多只有一套生效！</el-tag>
            <PaperInfo :platformId="formDetail.baseId" :activeName="activeName" type="1"></PaperInfo>
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-drawer>

    <el-drawer :title="'所属课程：' + formDetail.title" :visible.sync="drawer2" size="75%">
      <div class="ml20 mr20">
        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
          <el-tab-pane label="用户学习情况" name="sixty">
            <UserVideo :platformId="formDetail.baseId" :activeName="activeName"></UserVideo>
          </el-tab-pane>
          <el-tab-pane label="用户考试情况" name="seventy">
            <AnswerInfo :platformId="formDetail.baseId" :type="'0'.toString()" :activeName="activeName"></AnswerInfo>
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-drawer>
  </div>
</template>

<script>
  import {
    listPlatform,
    getPlatform,
    delPlatform,
    addPlatform,
    updatePlatform
  } from "@/api/business/platform";
  import PlatformChapter from "../platformChapter/index.vue";
  import PlatformVideo from "../platformVideo/index.vue";
  import PaperInfo from "../paperInfo/index.vue";
  import Preview from "../paperInfo/preview.vue";
  import OrderInfo from "../orderInfo/index.vue";
  import UserVideo from "../userVideo/index.vue";
  import AnswerInfo from "../answerInfo/index.vue";

  export default {
    name: "Platform",
    dicts: ['bmzt'],
    components: {
      PlatformChapter,
      PlatformVideo,
      PaperInfo,
      Preview,
      OrderInfo,
      UserVideo,
      AnswerInfo
    },
    data() {
      return {
        previewId: "",
        previewType: "",
        openPreview: false,

        drawer: false,
        drawer2: false,
        activeName: 'first',

        // 按钮loading
        buttonLoading: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 课程管理表格数据
        platformList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          title: undefined,
          picLink: undefined,
          applyStatus: undefined,
          sellPrice: undefined,
          sort: undefined,
        },
        // 表单参数
        form: {},
        // 表单参数
        formDetail: {},
        // 表单校验
        rules: {
          credits: [{
            required: true,
            message: "积分量不能为空",
            trigger: "blur"
          }],
          baseId: [{
            required: true,
            message: "主键不能为空",
            trigger: "blur"
          }],
          title: [{
            required: true,
            message: "标题不能为空",
            trigger: "blur"
          }],
          picLink: [{
            required: true,
            message: "图片不能为空",
            trigger: "blur"
          }],
          certTemplate: [{
            required: true,
            message: "证书模板不能为空",
            trigger: "blur"
          }],
          applyStatus: [{
            required: true,
            message: "课程状态不能为空",
            trigger: "change"
          }],
          sellPrice: [{
            required: true,
            message: "售卖价格不能为空",
            trigger: "blur"
          }],
          sort: [{
            required: true,
            message: "排序不能为空",
            trigger: "blur"
          }]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      handleClick(tab) {
        this.activeName = tab.name;
        console.log(tab.name);
      },
      chapterManagement(row) {
        this.activeName = 'first';
        this.drawer = true;
        this.formDetail = row;
      },
      userManagement(row) {
        this.activeName = 'sixty';
        this.drawer2 = true;
        this.formDetail = row;
      },
      /** 查询课程管理列表 */
      getList() {
        this.loading = true;
        listPlatform(this.queryParams).then(response => {
          this.platformList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      //试卷预览
      handlePreview(row) {
        this.title = "试卷预览";
        this.previewId = row.baseId;
        this.previewType = 2;
        this.openPreview = true;
        /*this.$router.push({
          name: "Preview",
          query: {
            id: row.baseId,
            type: 2
          }
        });*/
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          baseId: undefined,
          title: undefined,
          picLink: undefined,
          certTemplate: undefined,
          applyStatus: 0,
          sellPrice: undefined,
          sort: undefined,
          delFlag: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined,
          remark: undefined,
          modeType: 0,
          modeSubject: 0,
          subjectNum: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.baseId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加课程管理";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.loading = true;
        this.reset();
        const baseId = row.baseId || this.ids
        getPlatform(baseId).then(response => {
          this.loading = false;
          this.form = response.data;
          this.open = true;
          this.title = "修改课程管理";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.buttonLoading = true;
            if (this.form.baseId != null) {
              updatePlatform(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            } else {
              addPlatform(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const baseIds = row.baseId || this.ids;
        this.$modal.confirm('是否确认删除课程管理编号为"' + baseIds + '"的数据项？').then(() => {
          this.loading = true;
          return delPlatform(baseIds);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {}).finally(() => {
          this.loading = false;
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('business/platform/export', {
          ...this.queryParams
        }, `platform_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>

<style scoped>
  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }

  .clearfix:after {
    clear: both
  }

  .ml20 {
    margin-left: 20px;
  }

  .mr20 {
    margin-right: 20px;
  }

  .mt20 {
    margin-top: 20px;
  }

  .box-card {}
</style>
